package work;

import work.config.Constant;

import java.io.*;

public class LogReader {

    private BufferedReader reader;

    public static LogReader create(String name) {

        File file = new File(new File(Constant.work, "log"), name + ".log");

        if (!file.exists()) {
            return null;
        }

        LogReader logReader = new LogReader();

        try {
            logReader.reader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException ignore) {
            return null;
        }

        return logReader;

    }

    private LogReader() {

    }


    public String readLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            return null;
        }
    }

    public String readAll() {
        StringBuilder builder = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                builder.append(line).append("\n");
            }
            return builder.toString();
        } catch (IOException e) {
            return null;
        }

    }


    public void close() {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
